package com.nbgroup.service;

import com.nbgroup.dao.dayVisitorsRepositoty;
import com.nbgroup.entity.dayvisitors;
import com.nbgroup.utils.QueryUtil;
import com.nbgroup.utils.TimeTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by 18545 on 2019/7/4.
 */
@Service
public class DayVisitorsService {
    @Autowired
    QueryUtil queryUtil;
    @Autowired
    dayVisitorsRepositoty dayVisitorsRepositoty;

    public void findTodayVisitorsAndSaves() {
        Date date =new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String str = format.format(date);
        String sql = "SELECT * FROM dayvisitors WHERE today ='"+str+"'";
//        dayVisitors dayVisitors = queryUtil.query(sql, dayVisitors.class).get(0);
        List<dayvisitors> query = queryUtil.query(sql, dayvisitors.class);
        dayvisitors dayVisitors = null ;
        if(query.size()!=0){
         dayVisitors=query.get(0);
        }
        if(dayVisitors==null){
            String sql2 = " SELECT capacity FROM dayvisitors ORDER BY today DESC LIMIT 1";
            List<dayvisitors> query2 = queryUtil.query(sql2, dayvisitors.class);
            dayVisitors.setCapacity("2000");
            if (query2.size()>0){
                dayVisitors.setCapacity(query2.get(0).getCapacity());
            }
            dayVisitors.setToday(str);
            dayVisitors.setTouristVolume("1");
        }else {
            dayVisitors.setTouristVolume((Integer.parseInt(dayVisitors.getTouristVolume())+1)+"");
        }
        dayVisitorsRepositoty.save(dayVisitors);
    }

    public dayvisitors findTodayVisitorsAndSaveForHtml() {
        Date date =new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String str = format.format(date);
        String sql = "SELECT * FROM dayvisitors WHERE today ='"+str+"'";
        List<dayvisitors> query = queryUtil.query(sql, dayvisitors.class);
        if(query.size()==0){
            query.add(null);
        }
        dayvisitors dayVisitors =new dayvisitors();
        if(query.get(0)==null){
            String sql2 = " SELECT capacity FROM dayvisitors ORDER BY today DESC LIMIT 1";
            List<dayvisitors> query2 = queryUtil.query(sql2, dayvisitors.class);
            dayVisitors.setCapacity("2000");
            if (query2.size()>0){
                dayVisitors.setCapacity(query2.get(0).getCapacity());
            }
            dayVisitors.setToday(str);
//            dayVisitors.setCapacity("0");
            dayVisitors.setTouristVolume("0");
            dayvisitors save = dayVisitorsRepositoty.save(dayVisitors);
            dayVisitors=save;
        }else {
            dayVisitors=query.get(0);
        }
        return dayVisitors;
    }
    public String findWeek() {
        String sql = "SELECT * FROM dayvisitors WHERE today >=  \""+ TimeTools.getThisWeek(1)+" \" AND today <=  \""+TimeTools.getThisWeek(7)+"\"";
        List<dayvisitors> query = queryUtil.query(sql, dayvisitors.class);
        if(query.size()==0){
         return "0";
        }
        Integer i =0;
        for (dayvisitors dayvisitors: query) {
            i+=Integer.parseInt(dayvisitors.getTouristVolume());
        }
        return i+"";
    }

    public String findMonth() {
        String sql = "SELECT * FROM dayvisitors WHERE today >= \""+ TimeTools.getFirstDay()+"\" AND today <= \""+TimeTools.getEndDay()+"\"";
        List<dayvisitors> query = queryUtil.query(sql, dayvisitors.class);
        if(query.size()==0){
            return "0";
        }
        Integer i =0;
        for (dayvisitors dayvisitors: query) {
            i+=Integer.parseInt(dayvisitors.getTouristVolume());
        }
        return i+"";
    }

    public dayvisitors update(String capacity) {
        Date date =new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String str = format.format(date);
        String sql = "SELECT * FROM dayvisitors WHERE today ='"+str+"'";
        dayvisitors dayVisitors = queryUtil.query(sql, dayvisitors.class).get(0);
        dayVisitors.setCapacity(capacity);
        dayvisitors save = dayVisitorsRepositoty.saveAndFlush(dayVisitors);
        return save;
    }




}
